home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Objects / longobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  38.0 KB  |  1,841 lines

  1. /* Long (arbitrary precision) integer object implementation */
  2.  
  3. /* XXX The functional organization of this file is terrible */
  4.  
  5. #include "Python.h"
  6. #include "longintrepr.h"
  7. #include "mymath.h"
  8. #include "protos/longobject.h"
  9.  
  10. #include <assert.h>
  11. #include <ctype.h>
  12.  
  13. #define ABS(x) ((x) < 0 ? -(x) : (x))
  14.  
  15. /* Forward */
  16. static PyLongObject *long_normalize Py_PROTO((PyLongObject *));
  17. static PyLongObject *mul1 Py_PROTO((PyLongObject *, wdigit));
  18. static PyLongObject *muladd1 Py_PROTO((PyLongObject *, wdigit, wdigit));
  19. static PyLongObject *divrem1 Py_PROTO((PyLongObject *, wdigit, digit *));
  20. static PyObject *long_format Py_PROTO((PyObject *aa, int base, int addL));
  21.  
  22. static int ticker;    /* XXX Could be shared with ceval? */
  23.  
  24. #define SIGCHECK(PyTryBlock) \
  25.     if (--ticker < 0) { \
  26.         ticker = 100; \
  27.         if (PyErr_CheckSignals()) { PyTryBlock; } \
  28.     }
  29.  
  30. /* Normalize (remove leading zeros from) a long int object.
  31.    Doesn't attempt to free the storage--in most cases, due to the nature
  32.    of the algorithms used, this could save at most be one word anyway. */
  33.  
  34. static PyLongObject *
  35. long_normalize(v)
  36.     register PyLongObject *v;
  37. {
  38.     int j = ABS(v->ob_size);
  39.     register int i = j;
  40.     
  41.     while (i > 0 && v->ob_digit[i-1] == 0)
  42.         --i;
  43.     if (i != j)
  44.         v->ob_size = (v->ob_size < 0) ? -(i) : i;
  45.     return v;
  46. }
  47.  
  48. /* Allocate a new long int object with size digits.
  49.    Return NULL and set exception if we run out of memory. */
  50.  
  51. PyLongObject *
  52. _PyLong_New(size)
  53.     int size;
  54. {
  55.     return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
  56. }
  57.  
  58. /* Create a new long int object from a C long int */
  59.  
  60. PyObject *
  61. PyLong_FromLong(ival)
  62.     long ival;
  63. {
  64.     /* Assume a C long fits in at most 5 'digits' */
  65.     /* Works on both 32- and 64-bit machines */
  66.     PyLongObject *v = _PyLong_New(5);
  67.     if (v != NULL) {
  68.         unsigned long t = ival;
  69.         int i;
  70.         if (ival < 0) {
  71.             t = -ival;
  72.             v->ob_size = -(v->ob_size);
  73.           }
  74.         for (i = 0; i < 5; i++) {
  75.             v->ob_digit[i] = (digit) (t & MASK);
  76.             t >>= SHIFT;
  77.         }
  78.         v = long_normalize(v);
  79.     }
  80.     return (PyObject *)v;
  81. }
  82.  
  83. /* Create a new long int object from a C unsigned long int */
  84.  
  85. PyObject *
  86. PyLong_FromUnsignedLong(ival)
  87.     unsigned long ival;
  88. {
  89.     /* Assume a C long fits in at most 5 'digits' */
  90.     /* Works on both 32- and 64-bit machines */
  91.     PyLongObject *v = _PyLong_New(5);
  92.     if (v != NULL) {
  93.         unsigned long t = ival;
  94.         int i;
  95.         for (i = 0; i < 5; i++) {
  96.             v->ob_digit[i] = (digit) (t & MASK);
  97.             t >>= SHIFT;
  98.         }
  99.         v = long_normalize(v);
  100.     }
  101.     return (PyObject *)v;
  102. }
  103.  
  104. /* Create a new long int object from a C double */
  105.  
  106. PyObject *
  107. #ifdef MPW
  108. PyLong_FromDouble(double dval)
  109. #else
  110. PyLong_FromDouble(dval)
  111.     double dval;
  112. #endif /* MPW */
  113. {
  114.     PyLongObject *v;
  115.     double frac;
  116.     int i, ndig, expo, neg;
  117.     neg = 0;
  118.     if (dval && dval * 0.5 == dval) {
  119.         PyErr_SetString(PyExc_OverflowError,
  120.             "cannot convert float infinity to long");
  121.         return NULL;
  122.     }
  123.     if (dval < 0.0) {
  124.         neg = 1;
  125.         dval = -dval;
  126.     }
  127.     frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
  128.     if (expo <= 0)
  129.         return PyLong_FromLong(0L);
  130.     ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
  131.     v = _PyLong_New(ndig);
  132.     if (v == NULL)
  133.         return NULL;
  134.     frac = ldexp(frac, (expo-1) % SHIFT + 1);
  135.     for (i = ndig; --i >= 0; ) {
  136.         long bits = (long)frac;
  137.         v->ob_digit[i] = (digit) bits;
  138.         frac = frac - (double)bits;
  139.         frac = ldexp(frac, SHIFT);
  140.     }
  141.     if (neg)
  142.         v->ob_size = -(v->ob_size);
  143.     return (PyObject *)v;
  144. }
  145.  
  146. /* Get a C long int from a long int object.
  147.    Returns -1 and sets an error condition if overflow occurs. */
  148.  
  149. long
  150. PyLong_AsLong(vv)
  151.     PyObject *vv;
  152. {
  153.     /* This version by Tim Peters */
  154.     register PyLongObject *v;
  155.     unsigned long x, prev;
  156.     int i, sign;
  157.  
  158.     if (vv == NULL || !PyLong_Check(vv)) {
  159.         PyErr_BadInternalCall();
  160.         return -1;
  161.     }
  162.     v = (PyLongObject *)vv;
  163.     i = v->ob_size;
  164.     sign = 1;
  165.     x = 0;
  166.     if (i < 0) {
  167.         sign = -1;
  168.         i = -(i);
  169.     }
  170.     while (--i >= 0) {
  171.         prev = x;
  172.         x = (x << SHIFT) + v->ob_digit[i];
  173.         if ((x >> SHIFT) != prev)
  174.             goto overflow;
  175.     }
  176.     /* Haven't lost any bits, but if the sign bit is set we're in
  177.      * trouble *unless* this is the min negative number.  So,
  178.      * trouble iff sign bit set && (positive || some bit set other
  179.      * than the sign bit).
  180.      */
  181.     if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
  182.         goto overflow;
  183.     return (long)x * sign;
  184.  
  185.  overflow:
  186.     PyErr_SetString(PyExc_OverflowError,
  187.             "long int too long to convert");
  188.     return -1;
  189. }
  190.  
  191. /* Get a C long int from a long int object.
  192.    Returns -1 and sets an error condition if overflow occurs. */
  193.  
  194. unsigned long
  195. PyLong_AsUnsignedLong(vv)
  196.     PyObject *vv;
  197. {
  198.     register PyLongObject *v;
  199.     unsigned long x, prev;
  200.     int i;
  201.     
  202.     if (vv == NULL || !PyLong_Check(vv)) {
  203.         PyErr_BadInternalCall();
  204.         return (unsigned long) -1;
  205.     }
  206.     v = (PyLongObject *)vv;
  207.     i = v->ob_size;
  208.     x = 0;
  209.     if (i < 0) {
  210.         PyErr_SetString(PyExc_OverflowError,
  211.                "can't convert negative value to unsigned long");
  212.         return (unsigned long) -1;
  213.     }
  214.     while (--i >= 0) {
  215.         prev = x;
  216.         x = (x << SHIFT) + v->ob_digit[i];
  217.         if ((x >> SHIFT) != prev) {
  218.             PyErr_SetString(PyExc_OverflowError,
  219.                 "long int too long to convert");
  220.             return (unsigned long) -1;
  221.         }
  222.     }
  223.     return x;
  224. }
  225.  
  226. /* Get a C double from a long int object. */
  227.  
  228. double
  229. PyLong_AsDouble(vv)
  230.     PyObject *vv;
  231. {
  232.     register PyLongObject *v;
  233.     double x;
  234.     double multiplier = (double) (1L << SHIFT);
  235.     int i, sign;
  236.     
  237.     if (vv == NULL || !PyLong_Check(vv)) {
  238.         PyErr_BadInternalCall();
  239.         return -1;
  240.     }
  241.     v = (PyLongObject *)vv;
  242.     i = v->ob_size;
  243.     sign = 1;
  244.     x = 0.0;
  245.     if (i < 0) {
  246.         sign = -1;
  247.         i = -(i);
  248.     }
  249.     while (--i >= 0) {
  250.         x = x*multiplier + (double)v->ob_digit[i];
  251.     }
  252.     return x * sign;
  253. }
  254.  
  255. /* Create a new long (or int) object from a C pointer */
  256.  
  257. PyObject *
  258. PyLong_FromVoidPtr(p)
  259.     void *p;
  260. {
  261. #if SIZEOF_VOID_P == SIZEOF_LONG
  262.     return PyInt_FromLong((long)p);
  263. #else
  264.     /* optimize null pointers */
  265.     if ( p == NULL )
  266.         return PyInt_FromLong(0);
  267.  
  268.     /* we can assume that HAVE_LONG_LONG is true. if not, then the
  269.        configuration process should have bailed (having big pointers
  270.        without long longs seems non-sensical) */
  271.     return PyLong_FromLongLong((LONG_LONG)p);
  272. #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
  273. }
  274.  
  275. /* Get a C pointer from a long object (or an int object in some cases) */
  276.  
  277. void *
  278. PyLong_AsVoidPtr(vv)
  279.     PyObject *vv;
  280. {
  281.     /* This function will allow int or long objects. If vv is neither,
  282.        then the PyLong_AsLong*() functions will raise the exception:
  283.        PyExc_SystemError, "bad argument to internal function"
  284.     */
  285.  
  286. #if SIZEOF_VOID_P == SIZEOF_LONG
  287.     long x;
  288.  
  289.     if ( PyInt_Check(vv) )
  290.         x = PyInt_AS_LONG(vv);
  291.     else
  292.         x = PyLong_AsLong(vv);
  293. #else
  294.     /* we can assume that HAVE_LONG_LONG is true. if not, then the
  295.        configuration process should have bailed (having big pointers
  296.        without long longs seems non-sensical) */
  297.     LONG_LONG x;
  298.  
  299.     if ( PyInt_Check(vv) )
  300.         x = PyInt_AS_LONG(vv);
  301.     else
  302.         x = PyLong_AsLongLong(vv);
  303. #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
  304.  
  305.     if (x == -1 && PyErr_Occurred())
  306.         return NULL;
  307.     return (void *)x;
  308. }
  309.  
  310. #ifdef HAVE_LONG_LONG
  311. /*
  312.  * LONG_LONG support by Chris Herborth (chrish@qnx.com)
  313.  *
  314.  * For better or worse :-), I tried to follow the coding style already
  315.  * here.
  316.  */
  317.  
  318. /* Create a new long int object from a C LONG_LONG int */
  319.  
  320. PyObject *
  321. PyLong_FromLongLong(ival)
  322.     LONG_LONG ival;
  323. {
  324. #if SIZEOF_LONG_LONG == SIZEOF_LONG
  325.     /* In case the compiler is faking it. */
  326.     return PyLong_FromLong( (long)ival );
  327. #else
  328.     if( ival <= (LONG_LONG)LONG_MAX ) {
  329.         return PyLong_FromLong( (long)ival );
  330.     }
  331.     else if( ival <= (unsigned LONG_LONG)ULONG_MAX ) {
  332.         return PyLong_FromUnsignedLong( (unsigned long)ival );
  333.     }
  334.     else {
  335.         /* Assume a C LONG_LONG fits in at most 10 'digits'.
  336.          * Should be OK if we're assuming long fits in 5.
  337.          */
  338.         PyLongObject *v = _PyLong_New(10);
  339.  
  340.         if (v != NULL) {
  341.             unsigned LONG_LONG t = ival;
  342.             int i;
  343.             if (ival < 0) {
  344.                 t = -ival;
  345.                 v->ob_size = -(v->ob_size);
  346.               }
  347.  
  348.             for (i = 0; i < 10; i++) {
  349.                 v->ob_digit[i] = (digit) (t & MASK);
  350.                 t >>= SHIFT;
  351.             }
  352.  
  353.             v = long_normalize(v);
  354.         }
  355.  
  356.         return (PyObject *)v;
  357.     }
  358. #endif
  359. }
  360.  
  361. /* Create a new long int object from a C unsigned LONG_LONG int */
  362. PyObject *
  363. PyLong_FromUnsignedLongLong(ival)
  364.     unsigned LONG_LONG ival;
  365. {
  366. #if SIZEOF_LONG_LONG == SIZEOF_LONG
  367.     /* In case the compiler is faking it. */
  368.     return PyLong_FromUnsignedLong( (unsigned long)ival );
  369. #else
  370.     if( ival <= (unsigned LONG_LONG)ULONG_MAX ) {
  371.         return PyLong_FromUnsignedLong( (unsigned long)ival );
  372.     }
  373.     else {
  374.         /* Assume a C long fits in at most 10 'digits'. */
  375.         PyLongObject *v = _PyLong_New(10);
  376.  
  377.         if (v != NULL) {
  378.             unsigned LONG_LONG t = ival;
  379.             int i;
  380.             for (i = 0; i < 10; i++) {
  381.                 v->ob_digit[i] = (digit) (t & MASK);
  382.                 t >>= SHIFT;
  383.             }
  384.  
  385.             v = long_normalize(v);
  386.         }
  387.  
  388.         return (PyObject *)v;
  389.     }
  390. #endif
  391. }
  392.  
  393. /* Get a C LONG_LONG int from a long int object.
  394.    Returns -1 and sets an error condition if overflow occurs. */
  395.  
  396. LONG_LONG
  397. PyLong_AsLongLong(vv)
  398.     PyObject *vv;
  399. {
  400. #if SIZEOF_LONG_LONG == SIZEOF_LONG
  401.     /* In case the compiler is faking it. */
  402.     return (LONG_LONG)PyLong_AsLong( vv );
  403. #else
  404.     register PyLongObject *v;
  405.     LONG_LONG x, prev;
  406.     int i, sign;
  407.     
  408.     if (vv == NULL || !PyLong_Check(vv)) {
  409.         PyErr_BadInternalCall();
  410.         return -1;
  411.     }
  412.  
  413.     v = (PyLongObject *)vv;
  414.     i = v->ob_size;
  415.     sign = 1;
  416.     x = 0;
  417.  
  418.     if (i < 0) {
  419.         sign = -1;
  420.         i = -(i);
  421.     }
  422.  
  423.     while (--i >= 0) {
  424.         prev = x;
  425.         x = (x << SHIFT) + v->ob_digit[i];
  426.         if ((x >> SHIFT) != prev) {
  427.             PyErr_SetString(PyExc_OverflowError,
  428.                 "long int too long to convert");
  429.             return -1;
  430.         }
  431.     }
  432.  
  433.     return x * sign;
  434. #endif
  435. }
  436.  
  437. unsigned LONG_LONG
  438. PyLong_AsUnsignedLongLong(vv)
  439.     PyObject *vv;
  440. {
  441. #if SIZEOF_LONG_LONG == 4
  442.     /* In case the compiler is faking it. */
  443.     return (unsigned LONG_LONG)PyLong_AsUnsignedLong( vv );
  444. #else
  445.     register PyLongObject *v;
  446.     unsigned LONG_LONG x, prev;
  447.     int i;
  448.     
  449.     if (vv == NULL || !PyLong_Check(vv)) {
  450.         PyErr_BadInternalCall();
  451.         return (unsigned LONG_LONG) -1;
  452.     }
  453.  
  454.     v = (PyLongObject *)vv;
  455.     i = v->ob_size;
  456.     x = 0;
  457.  
  458.     if (i < 0) {
  459.         PyErr_SetString(PyExc_OverflowError,
  460.                "can't convert negative value to unsigned long");
  461.         return (unsigned LONG_LONG) -1;
  462.     }
  463.  
  464.     while (--i >= 0) {
  465.         prev = x;
  466.         x = (x << SHIFT) + v->ob_digit[i];
  467.         if ((x >> SHIFT) != prev) {
  468.             PyErr_SetString(PyExc_OverflowError,
  469.                 "long int too long to convert");
  470.             return (unsigned LONG_LONG) -1;
  471.         }
  472.     }
  473.  
  474.     return x;
  475. #endif
  476. }
  477. #endif /* HAVE_LONG_LONG */
  478.  
  479. /* Multiply by a single digit, ignoring the sign. */
  480.  
  481. static PyLongObject *
  482. mul1(a, n)
  483.     PyLongObject *a;
  484.     wdigit n;
  485. {
  486.     return muladd1(a, n, (digit)0);
  487. }
  488.  
  489. /* Multiply by a single digit and add a single digit, ignoring the sign. */
  490.  
  491. static PyLongObject *
  492. muladd1(a, n, extra)
  493.     PyLongObject *a;
  494.     wdigit n;
  495.     wdigit extra;
  496. {
  497.     int size_a = ABS(a->ob_size);
  498.     PyLongObject *z = _PyLong_New(size_a+1);
  499.     twodigits carry = extra;
  500.     int i;
  501.     
  502.     if (z == NULL)
  503.         return NULL;
  504.     for (i = 0; i < size_a; ++i) {
  505.         carry += (twodigits)a->ob_digit[i] * n;
  506.         z->ob_digit[i] = (digit) (carry & MASK);
  507.         carry >>= SHIFT;
  508.     }
  509.     z->ob_digit[i] = (digit) carry;
  510.     return long_normalize(z);
  511. }
  512.  
  513. /* Divide a long integer by a digit, returning both the quotient
  514.    (as function result) and the remainder (through *prem).
  515.    The sign of a is ignored; n should not be zero. */
  516.  
  517. static PyLongObject *
  518. divrem1(a, n, prem)
  519.     PyLongObject *a;
  520.     wdigit n;
  521.     digit *prem;
  522. {
  523.     int size = ABS(a->ob_size);
  524.     PyLongObject *z;
  525.     int i;
  526.     twodigits rem = 0;
  527.     
  528.     assert(n > 0 && n <= MASK);
  529.     z = _PyLong_New(size);
  530.     if (z == NULL)
  531.         return NULL;
  532.     for (i = size; --i >= 0; ) {
  533.         rem = (rem << SHIFT) + a->ob_digit[i];
  534.         z->ob_digit[i] = (digit) (rem/n);
  535.         rem %= n;
  536.     }
  537.     *prem = (digit) rem;
  538.     return long_normalize(z);
  539. }
  540.  
  541. /* Convert a long int object to a string, using a given conversion base.
  542.    Return a string object.
  543.    If base is 8 or 16, add the proper prefix '0' or '0x'. */
  544.  
  545. static PyObject *
  546. long_format(aa, base, addL)
  547.     PyObject *aa;
  548.     int base;
  549.         int addL;
  550. {
  551.     register PyLongObject *a = (PyLongObject *)aa;
  552.     PyStringObject *str;
  553.     int i;
  554.     int size_a = ABS(a->ob_size);
  555.     char *p;
  556.     int bits;
  557.     char sign = '\0';
  558.  
  559.     if (a == NULL || !PyLong_Check(a)) {
  560.         PyErr_BadInternalCall();
  561.         return NULL;
  562.     }
  563.     assert(base >= 2 && base <= 36);
  564.     
  565.     /* Compute a rough upper bound for the length of the string */
  566.     i = base;
  567.     bits = 0;
  568.     while (i > 1) {
  569.         ++bits;
  570.         i >>= 1;
  571.     }
  572.     i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
  573.     str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
  574.     if (str == NULL)
  575.         return NULL;
  576.     p = PyString_AS_STRING(str) + i;
  577.     *p = '\0';
  578.         if (addL)
  579.                 *--p = 'L';
  580.     if (a->ob_size < 0)
  581.         sign = '-';
  582.     
  583.     if (a->ob_size == 0) {
  584.         *--p = '0';
  585.     }
  586.     else if ((base & (base - 1)) == 0) {
  587.         /* JRH: special case for power-of-2 bases */
  588.         twodigits temp = a->ob_digit[0];
  589.         int bitsleft = SHIFT;
  590.         int rem;
  591.         int last = abs(a->ob_size);
  592.         int basebits = 1;
  593.         i = base;
  594.         while ((i >>= 1) > 1) ++basebits;
  595.         
  596.         i = 0;
  597.         for (;;) {
  598.             while (bitsleft >= basebits) {
  599.                 if ((temp == 0) && (i >= last - 1)) break;
  600.                 rem = temp & (base - 1);
  601.                 if (rem < 10)
  602.                     rem += '0';
  603.                 else
  604.                     rem += 'A' - 10;
  605.                 assert(p > PyString_AS_STRING(str));
  606.                 *--p = (char) rem;
  607.                 bitsleft -= basebits;
  608.                 temp >>= basebits;
  609.             }
  610.             if (++i >= last) {
  611.                 if (temp == 0) break;
  612.                 bitsleft = 99;
  613.                 /* loop again to pick up final digits */
  614.             }
  615.             else {
  616.                 temp = (a->ob_digit[i] << bitsleft) | temp;
  617.                 bitsleft += SHIFT;
  618.             }
  619.         }
  620.     }
  621.     else {
  622.         Py_INCREF(a);
  623.         do {
  624.             digit rem;
  625.             PyLongObject *temp = divrem1(a, (digit)base, &rem);
  626.             if (temp == NULL) {
  627.                 Py_DECREF(a);
  628.                 Py_DECREF(str);
  629.                 return NULL;
  630.             }
  631.             if (rem < 10)
  632.                 rem += '0';
  633.             else
  634.                 rem += 'A'-10;
  635.             assert(p > PyString_AS_STRING(str));
  636.             *--p = (char) rem;
  637.             Py_DECREF(a);
  638.             a = temp;
  639.             SIGCHECK({
  640.                 Py_DECREF(a);
  641.                 Py_DECREF(str);
  642.                 return NULL;
  643.             })
  644.         } while (ABS(a->ob_size) != 0);
  645.         Py_DECREF(a);
  646.     }
  647.  
  648.     if (base == 8) {
  649.         if (size_a != 0)
  650.             *--p = '0';
  651.     }
  652.     else if (base == 16) {
  653.         *--p = 'x';
  654.         *--p = '0';
  655.     }
  656.     else if (base != 10) {
  657.         *--p = '#';
  658.         *--p = '0' + base%10;
  659.         if (base > 10)
  660.             *--p = '0' + base/10;
  661.     }
  662.     if (sign)
  663.         *--p = sign;
  664.     if (p != PyString_AS_STRING(str)) {
  665.         char *q = PyString_AS_STRING(str);
  666.         assert(p > q);
  667.         do {
  668.         } while ((*q++ = *p++) != '\0');
  669.         q--;
  670.         _PyString_Resize((PyObject **)&str,
  671.                  (int) (q - PyString_AS_STRING(str)));
  672.     }
  673.     return (PyObject *)str;
  674. }
  675.  
  676. #if 0
  677. /* Convert a string to a long int object, in a given base.
  678.    Base zero implies a default depending on the number.
  679.    External linkage: used in compile.c and stropmodule.c. */
  680.  
  681. PyObject *
  682. long_scan(str, base)
  683.     char *str;
  684.     int base;
  685. {
  686.     return PyLong_FromString(str, (char **)NULL, base);
  687. }
  688. #endif
  689.  
  690. PyObject *
  691. PyLong_FromString(str, pend, base)
  692.     char *str;
  693.     char **pend;
  694.     int base;
  695. {
  696.     int sign = 1;
  697.     char *start, *orig_str = str;
  698.     PyLongObject *z;
  699.     
  700.     if ((base != 0 && base < 2) || base > 36) {
  701.         PyErr_SetString(PyExc_ValueError,
  702.                 "invalid base for long literal");
  703.         return NULL;
  704.     }
  705.     while (*str != '\0' && isspace(Py_CHARMASK(*str)))
  706.         str++;
  707.     if (*str == '+')
  708.         ++str;
  709.     else if (*str == '-') {
  710.         ++str;
  711.         sign = -1;
  712.     }
  713.     while (*str != '\0' && isspace(Py_CHARMASK(*str)))
  714.         str++;
  715.     if (base == 0) {
  716.         if (str[0] != '0')
  717.             base = 10;
  718.         else if (str[1] == 'x' || str[1] == 'X')
  719.             base = 16;
  720.         else
  721.             base = 8;
  722.     }
  723.     if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  724.         str += 2;
  725.     z = _PyLong_New(0);
  726.     start = str;
  727.     for ( ; z != NULL; ++str) {
  728.         int k = -1;
  729.         PyLongObject *temp;
  730.         
  731.         if (*str <= '9')
  732.             k = *str - '0';
  733.         else if (*str >= 'a')
  734.             k = *str - 'a' + 10;
  735.         else if (*str >= 'A')
  736.             k = *str - 'A' + 10;
  737.         if (k < 0 || k >= base)
  738.             break;
  739.         temp = muladd1(z, (digit)base, (digit)k);
  740.         Py_DECREF(z);
  741.         z = temp;
  742.     }
  743.     if (z == NULL)
  744.         return NULL;
  745.     if (str == start)
  746.         goto onError;
  747.     if (sign < 0 && z != NULL && z->ob_size != 0)
  748.         z->ob_size = -(z->ob_size);
  749.     if (*str == 'L' || *str == 'l')
  750.         str++;
  751.     while (*str && isspace(Py_CHARMASK(*str)))
  752.         str++;
  753.     if (*str != '\0')
  754.         goto onError;
  755.     if (pend)
  756.         *pend = str;
  757.     return (PyObject *) z;
  758.  
  759.  onError:
  760.     PyErr_Format(PyExc_ValueError, 
  761.              "invalid literal for long(): %.200s", orig_str);
  762.     Py_XDECREF(z);
  763.     return NULL;
  764. }
  765.  
  766. PyObject *
  767. PyLong_FromUnicode(u, length, base)
  768.     Py_UNICODE *u;
  769.     int length;
  770.     int base;
  771. {
  772.     char buffer[256];
  773.  
  774.     if (length >= sizeof(buffer)) {
  775.         PyErr_SetString(PyExc_ValueError,
  776.                 "long() literal too large to convert");
  777.         return NULL;
  778.     }
  779.     if (PyUnicode_EncodeDecimal(u, length, buffer, NULL))
  780.         return NULL;
  781.  
  782.     return PyLong_FromString(buffer, NULL, base);
  783. }
  784.  
  785. static PyLongObject *x_divrem
  786.     Py_PROTO((PyLongObject *, PyLongObject *, PyLongObject **));
  787. static PyObject *long_pos Py_PROTO((PyLongObject *));
  788. static int long_divrem Py_PROTO((PyLongObject *, PyLongObject *,
  789.     PyLongObject **, PyLongObject **));
  790.  
  791. /* Long division with remainder, top-level routine */
  792.  
  793. static int
  794. long_divrem(a, b, pdiv, prem)
  795.     PyLongObject *a, *b;
  796.     PyLongObject **pdiv;
  797.     PyLongObject **prem;
  798. {
  799.     int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
  800.     PyLongObject *z;
  801.     
  802.     if (size_b == 0) {
  803.         PyErr_SetString(PyExc_ZeroDivisionError,
  804.                 "long division or modulo");
  805.         return -1;
  806.     }
  807.     if (size_a < size_b ||
  808.         (size_a == size_b &&
  809.          a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
  810.         /* |a| < |b|. */
  811.         *pdiv = _PyLong_New(0);
  812.         Py_INCREF(a);
  813.         *prem = (PyLongObject *) a;
  814.         return 0;
  815.     }
  816.     if (size_b == 1) {
  817.         digit rem = 0;
  818.         z = divrem1(a, b->ob_digit[0], &rem);
  819.         if (z == NULL)
  820.             return -1;
  821.         *prem = (PyLongObject *) PyLong_FromLong((long)rem);
  822.     }
  823.     else {
  824.         z = x_divrem(a, b, prem);
  825.         if (z == NULL)
  826.             return -1;
  827.     }
  828.     /* Set the signs.
  829.        The quotient z has the sign of a*b;
  830.        the remainder r has the sign of a,
  831.        so a = b*z + r. */
  832.     if ((a->ob_size < 0) != (b->ob_size < 0))
  833.         z->ob_size = -(z->ob_size);
  834.     if (a->ob_size < 0 && (*prem)->ob_size != 0)
  835.         (*prem)->ob_size = -((*prem)->ob_size);
  836.     *pdiv = z;
  837.     return 0;
  838. }
  839.  
  840. /* Unsigned long division with remainder -- the algorithm */
  841.  
  842. static PyLongObject *
  843. x_divrem(v1, w1, prem)
  844.     PyLongObject *v1, *w1;
  845.     PyLongObject **prem;
  846. {
  847.     int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
  848.     digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1));
  849.     PyLongObject *v = mul1(v1, d);
  850.     PyLongObject *w = mul1(w1, d);
  851.     PyLongObject *a;
  852.     int j, k;
  853.     
  854.     if (v == NULL || w == NULL) {
  855.         Py_XDECREF(v);
  856.         Py_XDECREF(w);
  857.         return NULL;
  858.     }
  859.     
  860.     assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
  861.     assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
  862.     assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
  863.     
  864.     size_v = ABS(v->ob_size);
  865.     a = _PyLong_New(size_v - size_w + 1);
  866.     
  867.     for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
  868.         digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
  869.         twodigits q;
  870.         stwodigits carry = 0;
  871.         int i;
  872.         
  873.         SIGCHECK({
  874.             Py_DECREF(a);
  875.             a = NULL;
  876.             break;
  877.         })
  878.         if (vj == w->ob_digit[size_w-1])
  879.             q = MASK;
  880.         else
  881.             q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
  882.                 w->ob_digit[size_w-1];
  883.         
  884.         while (w->ob_digit[size_w-2]*q >
  885.                 ((
  886.                     ((twodigits)vj << SHIFT)
  887.                     + v->ob_digit[j-1]
  888.                     - q*w->ob_digit[size_w-1]
  889.                                 ) << SHIFT)
  890.                 + v->ob_digit[j-2])
  891.             --q;
  892.         
  893.         for (i = 0; i < size_w && i+k < size_v; ++i) {
  894.             twodigits z = w->ob_digit[i] * q;
  895.             digit zz = (digit) (z >> SHIFT);
  896.             carry += v->ob_digit[i+k] - z
  897.                 + ((twodigits)zz << SHIFT);
  898.             v->ob_digit[i+k] = carry & MASK;
  899.             carry = (carry >> SHIFT) - zz;
  900.         }
  901.         
  902.         if (i+k < size_v) {
  903.             carry += v->ob_digit[i+k];
  904.             v->ob_digit[i+k] = 0;
  905.         }
  906.         
  907.         if (carry == 0)
  908.             a->ob_digit[k] = (digit) q;
  909.         else {
  910.             assert(carry == -1);
  911.             a->ob_digit[k] = (digit) q-1;
  912.             carry = 0;
  913.             for (i = 0; i < size_w && i+k < size_v; ++i) {
  914.                 carry += v->ob_digit[i+k] + w->ob_digit[i];
  915.                 v->ob_digit[i+k] = carry & MASK;
  916.                 carry >>= SHIFT;
  917.             }
  918.         }
  919.     } /* for j, k */
  920.     
  921.     if (a == NULL)
  922.         *prem = NULL;
  923.     else {
  924.         a = long_normalize(a);
  925.         *prem = divrem1(v, d, &d);
  926.         /* d receives the (unused) remainder */
  927.         if (*prem == NULL) {
  928.             Py_DECREF(a);
  929.             a = NULL;
  930.         }
  931.     }
  932.     Py_DECREF(v);
  933.     Py_DECREF(w);
  934.     return a;
  935. }
  936.  
  937. /* Methods */
  938.  
  939. /* Forward */
  940. static void long_dealloc Py_PROTO((PyObject *));
  941. static PyObject *long_repr Py_PROTO((PyObject *));
  942. static int long_compare Py_PROTO((PyLongObject *, PyLongObject *));
  943. static long long_hash Py_PROTO((PyLongObject *));
  944.  
  945. static PyObject *long_add Py_PROTO((PyLongObject *, PyLongObject *));
  946. static PyObject *long_sub Py_PROTO((PyLongObject *, PyLongObject *));
  947. static PyObject *long_mul Py_PROTO((PyLongObject *, PyLongObject *));
  948. static PyObject *long_div Py_PROTO((PyLongObject *, PyLongObject *));
  949. static PyObject *long_mod Py_PROTO((PyLongObject *, PyLongObject *));
  950. static PyObject *long_divmod Py_PROTO((PyLongObject *, PyLongObject *));
  951. static PyObject *long_pow
  952.     Py_PROTO((PyLongObject *, PyLongObject *, PyLongObject *));
  953. static PyObject *long_neg Py_PROTO((PyLongObject *));
  954. static PyObject *long_pos Py_PROTO((PyLongObject *));
  955. static PyObject *long_abs Py_PROTO((PyLongObject *));
  956. static int long_nonzero Py_PROTO((PyLongObject *));
  957. static PyObject *long_invert Py_PROTO((PyLongObject *));
  958. static PyObject *long_lshift Py_PROTO((PyLongObject *, PyLongObject *));
  959. static PyObject *long_rshift Py_PROTO((PyLongObject *, PyLongObject *));
  960. static PyObject *long_and Py_PROTO((PyLongObject *, PyLongObject *));
  961. static PyObject *long_xor Py_PROTO((PyLongObject *, PyLongObject *));
  962. static PyObject *long_or Py_PROTO((PyLongObject *, PyLongObject *));
  963.  
  964. static void
  965. long_dealloc(v)
  966.     PyObject *v;
  967. {
  968.     PyObject_DEL(v);
  969. }
  970.  
  971. static PyObject *
  972. long_repr(v)
  973.     PyObject *v;
  974. {
  975.     return long_format(v, 10, 1);
  976. }
  977.  
  978. static PyObject *
  979. long_str(v)
  980.     PyObject *v;
  981. {
  982.     return long_format(v, 10, 0);
  983. }
  984.  
  985. static int
  986. long_compare(a, b)
  987.     PyLongObject *a, *b;
  988. {
  989.     int sign;
  990.     
  991.     if (a->ob_size != b->ob_size) {
  992.         if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
  993.             sign = 0;
  994.         else
  995.             sign = a->ob_size - b->ob_size;
  996.     }
  997.     else {
  998.         int i = ABS(a->ob_size);
  999.         while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
  1000.             ;
  1001.         if (i < 0)
  1002.             sign = 0;
  1003.         else {
  1004.             sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
  1005.             if (a->ob_size < 0)
  1006.                 sign = -sign;
  1007.         }
  1008.     }
  1009.     return sign < 0 ? -1 : sign > 0 ? 1 : 0;
  1010. }
  1011.  
  1012. static long
  1013. long_hash(v)
  1014.     PyLongObject *v;
  1015. {
  1016.     long x;
  1017.     int i, sign;
  1018.  
  1019.     /* This is designed so that Python ints and longs with the
  1020.        same value hash to the same value, otherwise comparisons
  1021.        of mapping keys will turn out weird */
  1022.     i = v->ob_size;
  1023.     sign = 1;
  1024.     x = 0;
  1025.     if (i < 0) {
  1026.         sign = -1;
  1027.         i = -(i);
  1028.     }
  1029.     while (--i >= 0) {
  1030.         /* Force a 32-bit circular shift */
  1031.         x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK);
  1032.         x += v->ob_digit[i];
  1033.     }
  1034.     x = x * sign;
  1035.     if (x == -1)
  1036.         x = -2;
  1037.     return x;
  1038. }
  1039.  
  1040.  
  1041. /* Add the absolute values of two long integers. */
  1042.  
  1043. static PyLongObject *x_add Py_PROTO((PyLongObject *, PyLongObject *));
  1044. static PyLongObject *
  1045. x_add(a, b)
  1046.     PyLongObject *a, *b;
  1047. {
  1048.     int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
  1049.     PyLongObject *z;
  1050.     int i;
  1051.     digit carry = 0;
  1052.     
  1053.     /* Ensure a is the larger of the two: */
  1054.     if (size_a < size_b) {
  1055.         { PyLongObject *temp = a; a = b; b = temp; }
  1056.         { int size_temp = size_a;
  1057.           size_a = size_b;
  1058.           size_b = size_temp; }
  1059.     }
  1060.     z = _PyLong_New(size_a+1);
  1061.     if (z == NULL)
  1062.         return NULL;
  1063.     for (i = 0; i < size_b; ++i) {
  1064.         carry += a->ob_digit[i] + b->ob_digit[i];
  1065.         z->ob_digit[i] = carry & MASK;
  1066.         /* The following assumes unsigned shifts don't
  1067.            propagate the sign bit. */
  1068.         carry >>= SHIFT;
  1069.     }
  1070.     for (; i < size_a; ++i) {
  1071.         carry += a->ob_digit[i];
  1072.         z->ob_digit[i] = carry & MASK;
  1073.         carry >>= SHIFT;
  1074.     }
  1075.     z->ob_digit[i] = carry;
  1076.     return long_normalize(z);
  1077. }
  1078.  
  1079. /* Subtract the absolute values of two integers. */
  1080.  
  1081. static PyLongObject *x_sub Py_PROTO((PyLongObject *, PyLongObject *));
  1082. static PyLongObject *
  1083. x_sub(a, b)
  1084.     PyLongObject *a, *b;
  1085. {
  1086.     int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
  1087.     PyLongObject *z;
  1088.     int i;
  1089.     int sign = 1;
  1090.     digit borrow = 0;
  1091.     
  1092.     /* Ensure a is the larger of the two: */
  1093.     if (size_a < size_b) {
  1094.         sign = -1;
  1095.         { PyLongObject *temp = a; a = b; b = temp; }
  1096.         { int size_temp = size_a;
  1097.           size_a = size_b;
  1098.           size_b = size_temp; }
  1099.     }
  1100.     else if (size_a == size_b) {
  1101.         /* Find highest digit where a and b differ: */
  1102.         i = size_a;
  1103.         while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
  1104.             ;
  1105.         if (i < 0)
  1106.             return _PyLong_New(0);
  1107.         if (a->ob_digit[i] < b->ob_digit[i]) {
  1108.             sign = -1;
  1109.             { PyLongObject *temp = a; a = b; b = temp; }
  1110.         }
  1111.         size_a = size_b = i+1;
  1112.     }
  1113.     z = _PyLong_New(size_a);
  1114.     if (z == NULL)
  1115.         return NULL;
  1116.     for (i = 0; i < size_b; ++i) {
  1117.         /* The following assumes unsigned arithmetic
  1118.            works module 2**N for some N>SHIFT. */
  1119.         borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
  1120.         z->ob_digit[i] = borrow & MASK;
  1121.         borrow >>= SHIFT;
  1122.         borrow &= 1; /* Keep only one sign bit */
  1123.     }
  1124.     for (; i < size_a; ++i) {
  1125.         borrow = a->ob_digit[i] - borrow;
  1126.         z->ob_digit[i] = borrow & MASK;
  1127.         borrow >>= SHIFT;
  1128.     }
  1129.     assert(borrow == 0);
  1130.     if (sign < 0)
  1131.         z->ob_size = -(z->ob_size);
  1132.     return long_normalize(z);
  1133. }
  1134.  
  1135. static PyObject *
  1136. long_add(a, b)
  1137.     PyLongObject *a;
  1138.     PyLongObject *b;
  1139. {
  1140.     PyLongObject *z;
  1141.     
  1142.     if (a->ob_size < 0) {
  1143.         if (b->ob_size < 0) {
  1144.             z = x_add(a, b);
  1145.             if (z != NULL && z->ob_size != 0)
  1146.                 z->ob_size = -(z->ob_size);
  1147.         }
  1148.         else
  1149.             z = x_sub(b, a);
  1150.     }
  1151.     else {
  1152.         if (b->ob_size < 0)
  1153.             z = x_sub(a, b);
  1154.         else
  1155.             z = x_add(a, b);
  1156.     }
  1157.     return (PyObject *)z;
  1158. }
  1159.  
  1160. static PyObject *
  1161. long_sub(a, b)
  1162.     PyLongObject *a;
  1163.     PyLongObject *b;
  1164. {
  1165.     PyLongObject *z;
  1166.     
  1167.     if (a->ob_size < 0) {
  1168.         if (b->ob_size < 0)
  1169.             z = x_sub(a, b);
  1170.         else
  1171.             z = x_add(a, b);
  1172.         if (z != NULL && z->ob_size != 0)
  1173.             z->ob_size = -(z->ob_size);
  1174.     }
  1175.     else {
  1176.         if (b->ob_size < 0)
  1177.             z = x_add(a, b);
  1178.         else
  1179.             z = x_sub(a, b);
  1180.     }
  1181.     return (PyObject *)z;
  1182. }
  1183.  
  1184. static PyObject *
  1185. long_mul(a, b)
  1186.     PyLongObject *a;
  1187.     PyLongObject *b;
  1188. {
  1189.     int size_a;
  1190.     int size_b;
  1191.     PyLongObject *z;
  1192.     int i;
  1193.     
  1194.     size_a = ABS(a->ob_size);
  1195.     size_b = ABS(b->ob_size);
  1196.     if (size_a > size_b) {
  1197.         /* we are faster with the small object on the left */
  1198.         int hold_sa = size_a;
  1199.         PyLongObject *hold_a = a;
  1200.         size_a = size_b;
  1201.         size_b = hold_sa;
  1202.         a = b;
  1203.         b = hold_a;
  1204.     }
  1205.     z = _PyLong_New(size_a + size_b);
  1206.     if (z == NULL)
  1207.         return NULL;
  1208.     for (i = 0; i < z->ob_size; ++i)
  1209.         z->ob_digit[i] = 0;
  1210.     for (i = 0; i < size_a; ++i) {
  1211.         twodigits carry = 0;
  1212.         twodigits f = a->ob_digit[i];
  1213.         int j;
  1214.         
  1215.         SIGCHECK({
  1216.             Py_DECREF(z);
  1217.             return NULL;
  1218.         })
  1219.         for (j = 0; j < size_b; ++j) {
  1220.             carry += z->ob_digit[i+j] + b->ob_digit[j] * f;
  1221.             z->ob_digit[i+j] = (digit) (carry & MASK);
  1222.             carry >>= SHIFT;
  1223.         }
  1224.         for (; carry != 0; ++j) {
  1225.             assert(i+j < z->ob_size);
  1226.             carry += z->ob_digit[i+j];
  1227.             z->ob_digit[i+j] = (digit) (carry & MASK);
  1228.             carry >>= SHIFT;
  1229.         }
  1230.     }
  1231.     if (a->ob_size < 0)
  1232.         z->ob_size = -(z->ob_size);
  1233.     if (b->ob_size < 0)
  1234.         z->ob_size = -(z->ob_size);
  1235.     return (PyObject *) long_normalize(z);
  1236. }
  1237.  
  1238. /* The / and % operators are now defined in terms of divmod().
  1239.    The expression a mod b has the value a - b*floor(a/b).
  1240.    The long_divrem function gives the remainder after division of
  1241.    |a| by |b|, with the sign of a.  This is also expressed
  1242.    as a - b*trunc(a/b), if trunc truncates towards zero.
  1243.    Some examples:
  1244.         a     b    a rem b        a mod b
  1245.         13     10     3         3
  1246.        -13     10    -3         7
  1247.         13    -10     3        -7
  1248.        -13    -10    -3        -3
  1249.    So, to get from rem to mod, we have to add b if a and b
  1250.    have different signs.  We then subtract one from the 'div'
  1251.    part of the outcome to keep the invariant intact. */
  1252.  
  1253. static int l_divmod Py_PROTO((PyLongObject *, PyLongObject *,
  1254.     PyLongObject **, PyLongObject **));
  1255. static int
  1256. l_divmod(v, w, pdiv, pmod)
  1257.     PyLongObject *v;
  1258.     PyLongObject *w;
  1259.     PyLongObject **pdiv;
  1260.     PyLongObject **pmod;
  1261. {
  1262.     PyLongObject *div, *mod;
  1263.     
  1264.     if (long_divrem(v, w, &div, &mod) < 0)
  1265.         return -1;
  1266.     if ((mod->ob_size < 0 && w->ob_size > 0) ||
  1267.         (mod->ob_size > 0 && w->ob_size < 0)) {
  1268.         PyLongObject *temp;
  1269.         PyLongObject *one;
  1270.         temp = (PyLongObject *) long_add(mod, w);
  1271.         Py_DECREF(mod);
  1272.         mod = temp;
  1273.         if (mod == NULL) {
  1274.             Py_DECREF(div);
  1275.             return -1;
  1276.         }
  1277.         one = (PyLongObject *) PyLong_FromLong(1L);
  1278.         if (one == NULL ||
  1279.             (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
  1280.             Py_DECREF(mod);
  1281.             Py_DECREF(div);
  1282.             Py_XDECREF(one);
  1283.             return -1;
  1284.         }
  1285.         Py_DECREF(one);
  1286.         Py_DECREF(div);
  1287.         div = temp;
  1288.     }
  1289.     *pdiv = div;
  1290.     *pmod = mod;
  1291.     return 0;
  1292. }
  1293.  
  1294. static PyObject *
  1295. long_div(v, w)
  1296.     PyLongObject *v;
  1297.     PyLongObject *w;
  1298. {
  1299.     PyLongObject *div, *mod;
  1300.     if (l_divmod(v, w, &div, &mod) < 0)
  1301.         return NULL;
  1302.     Py_DECREF(mod);
  1303.     return (PyObject *)div;
  1304. }
  1305.  
  1306. static PyObject *
  1307. long_mod(v, w)
  1308.     PyLongObject *v;
  1309.     PyLongObject *w;
  1310. {
  1311.     PyLongObject *div, *mod;
  1312.     if (l_divmod(v, w, &div, &mod) < 0)
  1313.         return NULL;
  1314.     Py_DECREF(div);
  1315.     return (PyObject *)mod;
  1316. }
  1317.  
  1318. static PyObject *
  1319. long_divmod(v, w)
  1320.     PyLongObject *v;
  1321.     PyLongObject *w;
  1322. {
  1323.     PyObject *z;
  1324.     PyLongObject *div, *mod;
  1325.     if (l_divmod(v, w, &div, &mod) < 0)
  1326.         return NULL;
  1327.     z = PyTuple_New(2);
  1328.     if (z != NULL) {
  1329.         PyTuple_SetItem(z, 0, (PyObject *) div);
  1330.         PyTuple_SetItem(z, 1, (PyObject *) mod);
  1331.     }
  1332.     else {
  1333.         Py_DECREF(div);
  1334.         Py_DECREF(mod);
  1335.     }
  1336.     return z;
  1337. }
  1338.  
  1339. static PyObject *
  1340. long_pow(a, b, c)
  1341.     PyLongObject *a;
  1342.     PyLongObject *b;
  1343.     PyLongObject *c;
  1344. {
  1345.     PyLongObject *z, *div, *mod;
  1346.     int size_b, i;
  1347.     
  1348.     size_b = b->ob_size;
  1349.     if (size_b < 0) {
  1350.         PyErr_SetString(PyExc_ValueError,
  1351.                 "long integer to the negative power");
  1352.         return NULL;
  1353.     }
  1354.     z = (PyLongObject *)PyLong_FromLong(1L);
  1355.     Py_INCREF(a);
  1356.     for (i = 0; i < size_b; ++i) {
  1357.         digit bi = b->ob_digit[i];
  1358.         int j;
  1359.     
  1360.         for (j = 0; j < SHIFT; ++j) {
  1361.             PyLongObject *temp;
  1362.         
  1363.             if (bi & 1) {
  1364.                 temp = (PyLongObject *)long_mul(z, a);
  1365.                 Py_DECREF(z);
  1366.                  if ((PyObject*)c!=Py_None && temp!=NULL) {
  1367.                      if (l_divmod(temp,c,&div,&mod) < 0) {
  1368.                         Py_DECREF(temp);
  1369.                         z = NULL;
  1370.                         goto error;
  1371.                     }
  1372.                      Py_XDECREF(div);
  1373.                      Py_DECREF(temp);
  1374.                      temp = mod;
  1375.                 }
  1376.                  z = temp;
  1377.                 if (z == NULL)
  1378.                     break;
  1379.             }
  1380.             bi >>= 1;
  1381.             if (bi == 0 && i+1 == size_b)
  1382.                 break;
  1383.             temp = (PyLongObject *)long_mul(a, a);
  1384.             Py_DECREF(a);
  1385.              if ((PyObject*)c!=Py_None && temp!=NULL) {
  1386.                  if (l_divmod(temp, c, &div, &mod) < 0) {
  1387.                     Py_DECREF(temp);
  1388.                     z = NULL;
  1389.                     goto error;
  1390.                 }
  1391.                  Py_XDECREF(div);
  1392.                  Py_DECREF(temp);
  1393.                  temp = mod;
  1394.             }
  1395.             a = temp;
  1396.             if (a == NULL) {
  1397.                 Py_DECREF(z);
  1398.                 z = NULL;
  1399.                 break;
  1400.             }
  1401.         }
  1402.         if (a == NULL || z == NULL)
  1403.             break;
  1404.     }
  1405.     Py_XDECREF(a);
  1406.     if ((PyObject*)c!=Py_None && z!=NULL) {
  1407.         if (l_divmod(z, c, &div, &mod) < 0) {
  1408.             Py_DECREF(z);
  1409.             z = NULL;
  1410.         }
  1411.         else {
  1412.             Py_XDECREF(div);
  1413.             Py_DECREF(z);
  1414.             z = mod;
  1415.         }
  1416.     }
  1417.   error:
  1418.     return (PyObject *)z;
  1419. }
  1420.  
  1421. static PyObject *
  1422. long_invert(v)
  1423.     PyLongObject *v;
  1424. {
  1425.     /* Implement ~x as -(x+1) */
  1426.     PyLongObject *x;
  1427.     PyLongObject *w;
  1428.     w = (PyLongObject *)PyLong_FromLong(1L);
  1429.     if (w == NULL)
  1430.         return NULL;
  1431.     x = (PyLongObject *) long_add(v, w);
  1432.     Py_DECREF(w);
  1433.     if (x == NULL)
  1434.         return NULL;
  1435.     if (x->ob_size != 0)
  1436.         x->ob_size = -(x->ob_size);
  1437.     return (PyObject *)x;
  1438. }
  1439.  
  1440. static PyObject *
  1441. long_pos(v)
  1442.     PyLongObject *v;
  1443. {
  1444.     Py_INCREF(v);
  1445.     return (PyObject *)v;
  1446. }
  1447.  
  1448. static PyObject *
  1449. long_neg(v)
  1450.     PyLongObject *v;
  1451. {
  1452.     PyLongObject *z;
  1453.     int i, n;
  1454.     n = ABS(v->ob_size);
  1455.     if (n == 0) {
  1456.         /* -0 == 0 */
  1457.         Py_INCREF(v);
  1458.         return (PyObject *) v;
  1459.     }
  1460.     z = _PyLong_New(ABS(n));
  1461.     if (z == NULL)
  1462.         return NULL;
  1463.     for (i = 0; i < n; i++)
  1464.         z->ob_digit[i] = v->ob_digit[i];
  1465.     z->ob_size = -(v->ob_size);
  1466.     return (PyObject *)z;
  1467. }
  1468.  
  1469. static PyObject *
  1470. long_abs(v)
  1471.     PyLongObject *v;
  1472. {
  1473.     if (v->ob_size < 0)
  1474.         return long_neg(v);
  1475.     else {
  1476.         Py_INCREF(v);
  1477.         return (PyObject *)v;
  1478.     }
  1479. }
  1480.  
  1481. static int
  1482. long_nonzero(v)
  1483.     PyLongObject *v;
  1484. {
  1485.     return ABS(v->ob_size) != 0;
  1486. }
  1487.  
  1488. static PyObject *
  1489. long_rshift(a, b)
  1490.     PyLongObject *a;
  1491.     PyLongObject *b;
  1492. {
  1493.     PyLongObject *z;
  1494.     long shiftby;
  1495.     int newsize, wordshift, loshift, hishift, i, j;
  1496.     digit lomask, himask;
  1497.     
  1498.     if (a->ob_size < 0) {
  1499.         /* Right shifting negative numbers is harder */
  1500.         PyLongObject *a1, *a2, *a3;
  1501.         a1 = (PyLongObject *) long_invert(a);
  1502.         if (a1 == NULL) return NULL;
  1503.         a2 = (PyLongObject *) long_rshift(a1, b);
  1504.         Py_DECREF(a1);
  1505.         if (a2 == NULL) return NULL;
  1506.         a3 = (PyLongObject *) long_invert(a2);
  1507.         Py_DECREF(a2);
  1508.         return (PyObject *) a3;
  1509.     }
  1510.     
  1511.     shiftby = PyLong_AsLong((PyObject *)b);
  1512.     if (shiftby == -1L && PyErr_Occurred())
  1513.         return NULL;
  1514.     if (shiftby < 0) {
  1515.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  1516.         return NULL;
  1517.     }
  1518.     wordshift = shiftby / SHIFT;
  1519.     newsize = ABS(a->ob_size) - wordshift;
  1520.     if (newsize <= 0) {
  1521.         z = _PyLong_New(0);
  1522.         return (PyObject *)z;
  1523.     }
  1524.     loshift = shiftby % SHIFT;
  1525.     hishift = SHIFT - loshift;
  1526.     lomask = ((digit)1 << hishift) - 1;
  1527.     himask = MASK ^ lomask;
  1528.     z = _PyLong_New(newsize);
  1529.     if (z == NULL)
  1530.         return NULL;
  1531.     if (a->ob_size < 0)
  1532.         z->ob_size = -(z->ob_size);
  1533.     for (i = 0, j = wordshift; i < newsize; i++, j++) {
  1534.         z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
  1535.         if (i+1 < newsize)
  1536.             z->ob_digit[i] |=
  1537.               (a->ob_digit[j+1] << hishift) & himask;
  1538.     }
  1539.     return (PyObject *) long_normalize(z);
  1540. }
  1541.  
  1542. static PyObject *
  1543. long_lshift(a, b)
  1544.     PyLongObject *a;
  1545.     PyLongObject *b;
  1546. {
  1547.     /* This version due to Tim Peters */
  1548.     PyLongObject *z;
  1549.     long shiftby;
  1550.     int oldsize, newsize, wordshift, remshift, i, j;
  1551.     twodigits accum;
  1552.     
  1553.     shiftby = PyLong_AsLong((PyObject *)b);
  1554.     if (shiftby == -1L && PyErr_Occurred())
  1555.         return NULL;
  1556.     if (shiftby < 0) {
  1557.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  1558.         return NULL;
  1559.     }
  1560.     if ((long)(int)shiftby != shiftby) {
  1561.         PyErr_SetString(PyExc_ValueError,
  1562.                 "outrageous left shift count");
  1563.         return NULL;
  1564.     }
  1565.     /* wordshift, remshift = divmod(shiftby, SHIFT) */
  1566.     wordshift = (int)shiftby / SHIFT;
  1567.     remshift  = (int)shiftby - wordshift * SHIFT;
  1568.  
  1569.     oldsize = ABS(a->ob_size);
  1570.     newsize = oldsize + wordshift;
  1571.     if (remshift)
  1572.         ++newsize;
  1573.     z = _PyLong_New(newsize);
  1574.     if (z == NULL)
  1575.         return NULL;
  1576.     if (a->ob_size < 0)
  1577.         z->ob_size = -(z->ob_size);
  1578.     for (i = 0; i < wordshift; i++)
  1579.         z->ob_digit[i] = 0;
  1580.     accum = 0;    
  1581.     for (i = wordshift, j = 0; j < oldsize; i++, j++) {
  1582.         accum |= a->ob_digit[j] << remshift;
  1583.         z->ob_digit[i] = (digit)(accum & MASK);
  1584.         accum >>= SHIFT;
  1585.     }
  1586.     if (remshift)
  1587.         z->ob_digit[newsize-1] = (digit)accum;
  1588.     else    
  1589.         assert(!accum);
  1590.     return (PyObject *) long_normalize(z);
  1591. }
  1592.  
  1593.  
  1594. /* Bitwise and/xor/or operations */
  1595.  
  1596. #define MAX(x, y) ((x) < (y) ? (y) : (x))
  1597. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  1598.  
  1599. static PyObject *long_bitwise Py_PROTO((PyLongObject *, int, PyLongObject *));
  1600. static PyObject *
  1601. long_bitwise(a, op, b)
  1602.     PyLongObject *a;
  1603.     int op; /* '&', '|', '^' */
  1604.     PyLongObject *b;
  1605. {
  1606.     digit maska, maskb; /* 0 or MASK */
  1607.     int negz;
  1608.     int size_a, size_b, size_z;
  1609.     PyLongObject *z;
  1610.     int i;
  1611.     digit diga, digb;
  1612.     PyObject *v;
  1613.     
  1614.     if (a->ob_size < 0) {
  1615.         a = (PyLongObject *) long_invert(a);
  1616.         maska = MASK;
  1617.     }
  1618.     else {
  1619.         Py_INCREF(a);
  1620.         maska = 0;
  1621.     }
  1622.     if (b->ob_size < 0) {
  1623.         b = (PyLongObject *) long_invert(b);
  1624.         maskb = MASK;
  1625.     }
  1626.     else {
  1627.         Py_INCREF(b);
  1628.         maskb = 0;
  1629.     }
  1630.     
  1631.     negz = 0;
  1632.     switch (op) {
  1633.     case '^':
  1634.         if (maska != maskb) {
  1635.             maska ^= MASK;
  1636.             negz = -1;
  1637.         }
  1638.         break;
  1639.     case '&':
  1640.         if (maska && maskb) {
  1641.             op = '|';
  1642.             maska ^= MASK;
  1643.             maskb ^= MASK;
  1644.             negz = -1;
  1645.         }
  1646.         break;
  1647.     case '|':
  1648.         if (maska || maskb) {
  1649.             op = '&';
  1650.             maska ^= MASK;
  1651.             maskb ^= MASK;
  1652.             negz = -1;
  1653.         }
  1654.         break;
  1655.     }
  1656.     
  1657.     /* JRH: The original logic here was to allocate the result value (z)
  1658.        as the longer of the two operands.  However, there are some cases
  1659.        where the result is guaranteed to be shorter than that: AND of two
  1660.        positives, OR of two negatives: use the shorter number.  AND with
  1661.        mixed signs: use the positive number.  OR with mixed signs: use the
  1662.        negative number.  After the transformations above, op will be '&'
  1663.        iff one of these cases applies, and mask will be non-0 for operands
  1664.        whose length should be ignored.
  1665.     */
  1666.  
  1667.     size_a = a->ob_size;
  1668.     size_b = b->ob_size;
  1669.     size_z = op == '&'
  1670.         ? (maska
  1671.            ? size_b
  1672.            : (maskb ? size_a : MIN(size_a, size_b)))
  1673.         : MAX(size_a, size_b);
  1674.     z = _PyLong_New(size_z);
  1675.     if (a == NULL || b == NULL || z == NULL) {
  1676.         Py_XDECREF(a);
  1677.         Py_XDECREF(b);
  1678.         Py_XDECREF(z);
  1679.         return NULL;
  1680.     }
  1681.     
  1682.     for (i = 0; i < size_z; ++i) {
  1683.         diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
  1684.         digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
  1685.         switch (op) {
  1686.         case '&': z->ob_digit[i] = diga & digb; break;
  1687.         case '|': z->ob_digit[i] = diga | digb; break;
  1688.         case '^': z->ob_digit[i] = diga ^ digb; break;
  1689.         }
  1690.     }
  1691.     
  1692.     Py_DECREF(a);
  1693.     Py_DECREF(b);
  1694.     z = long_normalize(z);
  1695.     if (negz == 0)
  1696.         return (PyObject *) z;
  1697.     v = long_invert(z);
  1698.     Py_DECREF(z);
  1699.     return v;
  1700. }
  1701.  
  1702. static PyObject *
  1703. long_and(a, b)
  1704.     PyLongObject *a;
  1705.     PyLongObject *b;
  1706. {
  1707.     return long_bitwise(a, '&', b);
  1708. }
  1709.  
  1710. static PyObject *
  1711. long_xor(a, b)
  1712.     PyLongObject *a;
  1713.     PyLongObject *b;
  1714. {
  1715.     return long_bitwise(a, '^', b);
  1716. }
  1717.  
  1718. static PyObject *
  1719. long_or(a, b)
  1720.     PyLongObject *a;
  1721.     PyLongObject *b;
  1722. {
  1723.     return long_bitwise(a, '|', b);
  1724. }
  1725.  
  1726. static int
  1727. long_coerce(pv, pw)
  1728.     PyObject **pv;
  1729.     PyObject **pw;
  1730. {
  1731.     if (PyInt_Check(*pw)) {
  1732.         *pw = PyLong_FromLong(PyInt_AsLong(*pw));
  1733.         Py_INCREF(*pv);
  1734.         return 0;
  1735.     }
  1736.     return 1; /* Can't do it */
  1737. }
  1738.  
  1739. static PyObject *
  1740. long_int(v)
  1741.     PyObject *v;
  1742. {
  1743.     long x;
  1744.     x = PyLong_AsLong(v);
  1745.     if (PyErr_Occurred())
  1746.         return NULL;
  1747.     return PyInt_FromLong(x);
  1748. }
  1749.  
  1750. static PyObject *
  1751. long_long(v)
  1752.     PyObject *v;
  1753. {
  1754.     Py_INCREF(v);
  1755.     return v;
  1756. }
  1757.  
  1758. static PyObject *
  1759. long_float(v)
  1760.     PyObject *v;
  1761. {
  1762.     double result;
  1763.     PyFPE_START_PROTECT("long_float", return 0)
  1764.     result = PyLong_AsDouble(v);
  1765.     PyFPE_END_PROTECT(result)
  1766.     return PyFloat_FromDouble(result);
  1767. }
  1768.  
  1769. static PyObject *
  1770. long_oct(v)
  1771.     PyObject *v;
  1772. {
  1773.     return long_format(v, 8, 1);
  1774. }
  1775.  
  1776. static PyObject *
  1777. long_hex(v)
  1778.     PyObject *v;
  1779. {
  1780.     return long_format(v, 16, 1);
  1781. }
  1782.  
  1783.  
  1784. #define UF (unaryfunc)
  1785. #define BF (binaryfunc)
  1786. #define TF (ternaryfunc)
  1787. #define IF (inquiry)
  1788.  
  1789. static PyNumberMethods long_as_number = {
  1790.     BF long_add,    /*nb_add*/
  1791.     BF long_sub,    /*nb_subtract*/
  1792.     BF long_mul,    /*nb_multiply*/
  1793.     BF long_div,    /*nb_divide*/
  1794.     BF long_mod,    /*nb_remainder*/
  1795.     BF long_divmod,    /*nb_divmod*/
  1796.     TF long_pow,    /*nb_power*/
  1797.     UF long_neg,    /*nb_negative*/
  1798.     UF long_pos,    /*tp_positive*/
  1799.     UF long_abs,    /*tp_absolute*/
  1800.     IF long_nonzero,/*tp_nonzero*/
  1801.     UF long_invert,    /*nb_invert*/
  1802.     BF long_lshift,    /*nb_lshift*/
  1803.     BF long_rshift,    /*nb_rshift*/
  1804.     BF long_and,    /*nb_and*/
  1805.     BF long_xor,    /*nb_xor*/
  1806.     BF long_or,    /*nb_or*/
  1807.     (int (*) Py_FPROTO((PyObject **, PyObject **)))
  1808.     (coercion)long_coerce, /*nb_coerce*/
  1809.     UF long_int,    /*nb_int*/
  1810.     UF long_long,    /*nb_long*/
  1811.     UF long_float,    /*nb_float*/
  1812.     UF long_oct,    /*nb_oct*/
  1813.     UF long_hex,    /*nb_hex*/
  1814. };
  1815.  
  1816. PyTypeObject PyLong_Type = {
  1817.     PyObject_HEAD_INIT(&PyType_Type)
  1818.     0,
  1819.     "long int",
  1820. #ifdef __SASC
  1821.     sizeof(struct _longobject) - sizeof(digit),
  1822. #else
  1823.     sizeof(PyLongObject) - sizeof(digit),
  1824. #endif
  1825.     sizeof(digit),
  1826.     (destructor)long_dealloc, /*tp_dealloc*/
  1827.     0,        /*tp_print*/
  1828.     0,        /*tp_getattr*/
  1829.     0,        /*tp_setattr*/
  1830.     (int (*) Py_FPROTO((PyObject *, PyObject *)))
  1831.     (cmpfunc)long_compare, /*tp_compare*/
  1832.     (reprfunc)long_repr, /*tp_repr*/
  1833.     &long_as_number,/*tp_as_number*/
  1834.     0,        /*tp_as_sequence*/
  1835.     0,        /*tp_as_mapping*/
  1836.     (long (*) Py_FPROTO((PyObject *)))
  1837.     (hashfunc)long_hash, /*tp_hash*/
  1838.         0,              /*tp_call*/
  1839.         (reprfunc)long_str, /*tp_str*/
  1840. };
  1841.